<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<%@ include file="/jsp/common/common.jsp"%>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script type="text/javascript"
			src="<c:url value="/static/js/common.js"/>"></script>		
<script type="text/javascript">

    var editIndex = undefined;

	function initList(listDisplayEId) {
		$('#' + listDisplayEId).datagrid(
				{
					nowrap : false,
					striped : true,
					collapsible : true,
					remoteSort : true,
					notSelectByRow : false,
					/* frozenColumns : [ [ {
						field : 'ids',
						checkbox : true
					} ] ], */
					toolbar : [
                               {
                                   id : listDisplayEId + '_btn_del',
                                   text : '删除',
                                   iconCls : 'icon-cancel',
                                   handler : function() {
        
                                           var row = $('#'+listDisplayEId).datagrid('getSelections');      
                                           if(row.length == 0){
                                               $.messager.alert('提示','请选择一条记录操作！','info');
                                               return;
                                           }else{
                                                   var str = "ordId=" + row[0]['ordId']+"&ordMaterId=" + row[0]['ordMaterId']+"&ordSupId=" + row[0]['ordSupId']; 
                                                   $.messager.confirm('提示', '是否删除该条记录？', function(r){
                                                       if (r){         
                                                           $.ajax({type:'post',url:"<c:url value='/order/deleteOrderSupperMater.do'/>",data:str,dataType:"json",//设置查询参数及提交路径
                                                               success:function(data){
                                                                   if(data.success){
                                                                        $.messager.alert("提示", "删除成功！", 'info');
                                                                        $('#'+listDisplayEId).datagrid('reload');
                                                                        }else{
                                                                             $.messager.alert("提示", data.msg, 'warning');
                                                                   }
                                                               },
                                                               error:function(){
                                                                   $.messager.alert("错误", "系统错误，请联系管理员！", 'error');
                                                               }
                                                           });
                                                       }
                                                   $('#'+listDisplayEId).datagrid('reload');
                                                   $('#'+listDisplayEId).datagrid('clearSelections');
                                                   });
                                            }
                                  }
                               },
                               {
                                   id : listDisplayEId + '_btn_edit',
                                   text : '更改供应商',
                                   iconCls : 'icon-edit',
                                   handler : function() {
                                           if($('#supId').combobox('getValue') ==''){
                                               $.messager.alert('提示','请选择供货商','info');
                                               return;
                                           }
                                           var row = $('#'+listDisplayEId).datagrid('getSelections');      
                                           if(row.length == 0){
                                               $.messager.alert('提示','请选择一条记录操作！','info');
                                               return;
                                           }else{
                                        	   
                                        	   for (var i = 0 ; i < row.length;i++){
                                        		   var userList = new Array(); 
                                                   var jsonFater;
                                                   for (var i = 0 ; i < row.length;i++){
                                                       var jsonSun = "{ordId:'"+row[i]['ordId']+"',ordMaterId:'"+row[i]['ordMaterId']+"',lastOrdSupId:'"+row[i]['ordSupId']+"',ordSupId:'"+$('#supId').combobox('getValue')+"',ordCustId:'"+row[i]['ordCustId']+"'}";
                                                       userList.push(jsonSun);
                                                   }
                                                   jsonFater = JSON.stringify(userList);
                                                   var str = "json="+jsonFater;    
                                                   document.getElementsByName("orderList")[0].value = jsonFater;
                                                   
                                                   updateSup();
                                                   
                                               }
                                            }
                                  }
                               }
							],
					onLoadError : function() {
						$.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
					},
					onClickRow : onClickRow
				});

	};
	
	$(document).ready(function() {
		initList('listDisplay');
	});
	function updateSup(){
        $('#AddFoodForm').form('submit',
        {
            url:'<c:url value='/order/updateOrderSupperMater.do'/>', //查询提交路径
            onSubmit:function()//提交按钮相应函数
            {
                return $('#AddFoodForm').form('validate');
            },
            success:function(data){
                data = eval( "("+data+")" );
                if(data.success){
                    $.messager.alert("提示", "更改成功！", 'info');
                    $('#listDisplay').datagrid('reload');
                    $('#listDisplay').datagrid('reload');
                    $('#listDisplay').datagrid('clearSelections');
                    
                }else{
                    var message = "";
                    if(data.msg == "msg002"){
                        message = "所选择日期的材料订单已经下单完成，如需重新选择请删除已有订单。";
                    }else if(data.msg == "msg001"){
                        message = "选择材料的时间已过，请联系采购部。";
                    }
                    $.messager.alert("提示", message, 'warning');
                }
            },
            error:function(){
                $.messager.alert("错误", "系统错误，请联系管理员！", 'error');
                return;
            }
        });
        
    }
	$(function() {        
        // 下拉框选择控件，下拉框的内容是动态查询数据库信息  
        $('#supId').combobox({   
                url:"<c:url value='/store/pulldownSupId.do?space=0'/>",   
                editable:true, //不可编辑状态  
                cache: false,  
                panelHeight: '150',  
                valueField:'supId',     
                textField:'supName',  
                required:true
        });  
  });  
	function  goDetailedContent(val,row){
		return '<a href="#" onclick="constructionManager(\'' + row.id+ '\')">'+val+'</a>  '
	}
	
	function onClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#listDisplay').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#listDisplay').datagrid('selectRow', editIndex);
			}
		}
	}
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#listDisplay').datagrid('validateRow', editIndex)){
			var ed = $('#listDisplay').datagrid('getEditor', {index:editIndex,field:'level'});
			var levelname = $(ed.target).combobox('getText');
			$('#listDisplay').datagrid('getRows')[editIndex]['levelname'] = levelname;
			$('#listDisplay').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function cellStyler(value, row, index) {
        if (row['leaveMessage'] != '') {
            return 'background-color:#CCFFCC;';
        }
    }
</script>
</head>
<!-- 
        页面详细说明
        审核页面 和店长的我的菜品订单页面  点击详细信息   如果 是采购 材料 则跳转此页面
 -->
<body>
    <table width="98%">
        <tr>
            <td>
                <form id="AddFoodForm" name="AddFoodForm" method="post">
                <div
                    style="height: auto; width: 1180px; background: #fafafa; padding: 2px;"
                    class="easyui-panel" title="&nbsp&nbsp&nbsp&nbsp&nbsp修改信息"
                    collapsible="true" minimizable="false" maximizable="false"
                    closable="false">
                    <table cellpadding="5">
                        <tr>
                            <td>供货商:</td>
                            <td>
                                <input class="easyui-combobox"   name="supId" id="supId"  data-options="required:true">
                            </td>
                        </tr>
                        <tr>
                            <td><input class="easyui-validatebox" type="hidden" name="ordId" required="true" validType="length[1,30]" value="${ordId }" />
                            </td>
                            <td><input class="easyui-validatebox" type="hidden" name="orderList"  /></td>
                            
                        </tr>
                    </table>
                </div>
                </form>
            </td>
        </tr>
        <tr>
            <td>
                <table id="listDisplay" title="详细材料列表" singleSelect=false
                    iconCls="icon-list" idField="ordSupMaterId"
                    url="<c:url value="/order/findSelectMaterDetailed.do?oid=${ordId }"/>"
                    style="width: 800px; width: 1180px;" loadMsg="请稍候......"
                    pagination="true" pageSize="10"
                    pageList="[10,20,30,40,50,100,150,200]" sortName="roleId"
                    rownumbers="true" sortOrder="asc">
                    <thead>
                        <tr>
                            <th field="id" width="25" align="center" checkbox="true"></th>
                            <th field="ordSupMaterId" width="80" sortable="true" hidden="true">ID</th>
                            <th field="ordId" width="60" sortable="true" styler="cellStyler">订单ID</th>
                            <th field="ordCustId" width="60" sortable="true" styler="cellStyler">客户ID</th>
                            <th field="ordCustName" width="80" sortable="true" styler="cellStyler">客户名</th>
                            <th field="ordSupId" width="60" sortable="true" styler="cellStyler">供货商ID</th>
                            <th field="ordSupName" width="80" sortable="true" styler="cellStyler">供货商名</th>
                            <th field="ordMaterId" width="60" formatter="formatDGField"
                                sortable="true" styler="cellStyler">材料id</th>
                            <th field="materType" width="60" formatter="formatFoodType1"
                                sortable="true" styler="cellStyler">种类</th>
                            <th field="ordMaterName" width="120" formatter="formatDGField"
                                sortable="true" styler="cellStyler">材料名</th>
                            
                            <th field="brands" width="60" sortable="true" styler="cellStyler">品牌 </th>
                            <th field="specifications" width="60" sortable="true" styler="cellStyler">规格 </th>
                            <th field="ordMaterWeight" width="60" formatter="formatDGField"
                                sortable="true"styler="cellStyler">材料重量</th>
                            <th field="unit" width="60"  sortable="true" styler="cellStyler">单位 </th>
                            <th field="ordMaterValue" width="100" sortable="true"
                                editor="true" styler="cellStyler">材料价格</th>
                            <th field="ordMaterLevel" width="100" formatter="formatFoodType"
                                sortable="true" editor="true" hidden="true" styler="cellStyler">品级</th>
                            <th field="leaveMessage" width="80" sortable="true"  styler="cellStyler">留言</th>   
                            <th field="submitOrdTime" width="100" sortable="true"
                                editor="true" styler="cellStyler">最后提交时间</th>
                        </tr>
                    </thead>
                    </td>
                    </tr>
                </table>
    </table>
</body>
</html>